/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.web.formmetadata.i18n.component.strategy.i18nresourcestrategy;

import com.inspur.edp.lcm.metadata.api.entity.I18nResourceItem;
import com.inspur.edp.lcm.metadata.api.entity.I18nResourceItemCollection;
import com.inspur.edp.web.common.utility.StringUtility;
import com.inspur.edp.web.formmetadata.i18n.I18nResourceItemManager;
import com.inspur.edp.web.formmetadata.i18n.component.ComponentUtility;
import com.inspur.edp.web.formmetadata.i18n.component.I18nResourceUtility;
import com.inspur.edp.web.formmetadata.i18n.constant.I18nResourceConstant;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 过滤条资源管理策略
 */
public class ListFilterI18nResourceStrategy extends AbstractI18nResourceStrategy {
    @Override
    protected I18nResourceItemCollection extractAttributeI18nResourceItemCollection(String i18nResourceItemBaseId, HashMap<String, Object> currentComponent) {
        // 从filterList属性中提取多语字段
        I18nResourceItemCollection i18nResourceItemCollection = new I18nResourceItemCollection();

        I18nResourceItemCollection fieldConfigsI18nResourceItemCollection = extractFilterListI18nResourceItemCollection(i18nResourceItemBaseId, currentComponent);
        if (fieldConfigsI18nResourceItemCollection.size() > 0) {
            i18nResourceItemCollection.addRange(fieldConfigsI18nResourceItemCollection);
        }

        return i18nResourceItemCollection;

    }

    /**
     * 提取过滤条列表资源项
     *
     * @param i18nResourceItemBaseId
     * @param currentComponent
     * @return
     */
    private I18nResourceItemCollection extractFilterListI18nResourceItemCollection(String i18nResourceItemBaseId, HashMap<String, Object> currentComponent) {
        I18nResourceItemCollection i18nResourceItemCollection = new I18nResourceItemCollection();
        // 从filterList属性中提取多语资源
        ArrayList<HashMap<String, Object>> childComponents = ComponentUtility.getInstance().GetFilterList(currentComponent);
        I18nResourceItemCollection childComponentsI18nResourceItemCollection = extractChildComponentsI18nResourceItemCollection(i18nResourceItemBaseId, currentComponent, childComponents);
        if (childComponentsI18nResourceItemCollection != null && childComponentsI18nResourceItemCollection.size() > 0) {
            i18nResourceItemCollection.addRange(childComponentsI18nResourceItemCollection);
        }

        return i18nResourceItemCollection;
    }

    private I18nResourceItemCollection extractChildComponentsI18nResourceItemCollection(String i18nResourceItemBaseId, HashMap<String, Object> currentComponent, ArrayList<HashMap<String, Object>> childComponentList) {
        I18nResourceItemCollection i18nResourceItemCollection = new I18nResourceItemCollection();
        String currentComponentType = ComponentUtility.getInstance().getType(currentComponent);
        String currentComponentId = ComponentUtility.getInstance().getId(currentComponent);
        if (StringUtility.isNullOrEmpty(currentComponentType) || StringUtility.isNullOrEmpty(currentComponentId)) {
            return null;
        }

        // 针对过滤条中的具体项进行处理
        for (HashMap<String, Object> childComponent : childComponentList) {
            //获取具体项的id
            String idAtributeValue = ComponentUtility.getInstance().getId(childComponent);
            // 获取具体项的name
            String nameAtributeValue = ComponentUtility.getInstance().getName(childComponent);
            if (!StringUtility.isNullOrEmpty(idAtributeValue)) {
                String componentId = idAtributeValue;
                String generatedComponentId = currentComponentType + "/" + currentComponentId + "/" + componentId;
                // 将具体项的name 添加至资源项中
                I18nResourceItem i18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, generatedComponentId, nameAtributeValue, nameAtributeValue);
                this.addInCollection(i18nResourceItemCollection, i18nResourceItem);

                //提取placeholder属性参数
                String placeHolderName = ComponentUtility.getInstance().GetPlaceHolderName(childComponent);
                String placeHolderValue = ComponentUtility.getInstance().getPlaceHolder(childComponent);
                String generatedPlaceHolderComponentId = currentComponentType + "/" + currentComponentId + "/" + componentId + "/" + placeHolderName;
                I18nResourceItem i18nPlaceholderResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, generatedPlaceHolderComponentId, placeHolderValue, placeHolderValue);
                this.addInCollection(i18nResourceItemCollection, i18nPlaceholderResourceItem);


                // 子组件的属性中包含多语资源项
                I18nResourceItemCollection childComponentAttributeI18nResourceItemCollection = extractChildComponentAttributeI18nResourceItemCollection(i18nResourceItemBaseId, generatedComponentId, childComponent);
                if (childComponentAttributeI18nResourceItemCollection != null && childComponentAttributeI18nResourceItemCollection.size() > 0) {
                    i18nResourceItemCollection.addRange(childComponentAttributeI18nResourceItemCollection);
                }
            }


        }

        return i18nResourceItemCollection;
    }

    private I18nResourceItemCollection extractChildComponentAttributeI18nResourceItemCollection(String i18nResourceItemBaseId, String componentI18nResourceItemBaseId, HashMap<String, Object> currentComponent) {
        I18nResourceItemCollection i18nResourceItemCollection = new I18nResourceItemCollection();

        //从control属性获取对象
        I18nResourceItemCollection controlAttributeI18nResourceItemCollection = extractControlAttributeI18nResourceItemCollection(i18nResourceItemBaseId, componentI18nResourceItemBaseId, currentComponent);
        if (controlAttributeI18nResourceItemCollection != null && controlAttributeI18nResourceItemCollection.size() > 0) {
            i18nResourceItemCollection.addRange(controlAttributeI18nResourceItemCollection);
        }

        return i18nResourceItemCollection;
    }

    private I18nResourceItemCollection extractControlAttributeI18nResourceItemCollection(String i18nResourceItemBaseId, String componentI18nResourceItemBaseId, HashMap<String, Object> currentComponent) {
        I18nResourceItemCollection i18nResourceItemCollection = new I18nResourceItemCollection();
        // 获取对应的control属性值
        HashMap<String, Object> controlAttributeObject = ComponentUtility.getInstance().GetControlAttributeObject(currentComponent);

        String controlAttributeName = ComponentUtility.getInstance().GetControlAttributeName(currentComponent);
        componentI18nResourceItemBaseId += I18nResourceConstant.SECOND_LEVEL_DELIMITER + controlAttributeName;

        String strValueFieldKey = ComponentUtility.getInstance().getValueWithKey(controlAttributeObject, I18nResourceConstant.ValueFieldKey);
        String strTextFieldKey = ComponentUtility.getInstance().getValueWithKey(controlAttributeObject, I18nResourceConstant.TextFieldKey);

        // 从enumValues中提取多语资源项
        ArrayList<HashMap<String, Object>> enumValueCollection = ComponentUtility.getInstance().GetEnumValues(controlAttributeObject);
        String enumValuesAttributeName = ComponentUtility.getInstance().GetEnumValuesName(controlAttributeObject);

        I18nResourceItemCollection enumValuesI18nResourceItemCollection = I18nResourceUtility.GetInstance().ExtractEnumDataI18nResourceItemCollection(i18nResourceItemBaseId, componentI18nResourceItemBaseId, enumValueCollection, enumValuesAttributeName, strValueFieldKey, strTextFieldKey);
        if (enumValuesI18nResourceItemCollection != null && enumValuesI18nResourceItemCollection.size() > 0) {
            i18nResourceItemCollection.addRange(enumValuesI18nResourceItemCollection);
        }

        return i18nResourceItemCollection;
    }

}
